package nongchang.kuaijiecaifu.com.ncapp_android.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.os.CountDownTimer;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.trello.rxlifecycle.RxLifecycle;

import butterknife.BindView;
import butterknife.ButterKnife;
import ezy.lite.util.DateTime;
import ezy.lite.util.UI;
import ezy.widget.view.AutofitImageView;
import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.common.adapter.ItemAdapter;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.API;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.PanicBuy;
import nongchang.kuaijiecaifu.com.ncapp_android.util.countdown.RxCountDown;

/**
 * Created by Administrator on 2016/4/27 0027.
 */
public class PanicAdapter extends ItemAdapter<PanicBuy, PanicAdapter.PanicViewHolder> {
    private Context context;

    public PanicAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public PanicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PanicViewHolder(inflate(R.layout.item_panic_buy, parent));
    }

    @Override
    public void onBindViewHolder(PanicViewHolder holder, int position) {
        PanicBuy item=getItem(position);
        Glide.with(context).load(item.image).into(holder.imgShop);
        holder.txtTitle.setText(item.title);
//        countdown(holder.txtTime,item.time);
//        RxCountDown.countdown((int)item.time).compose(RxLifecycle.bindView(holder.itemView)).doOnCompleted(() -> {
//        }).subscribe(seconds -> {
//            UI.setTvHtml(holder.txtTime, R.string.cashier_pay_countdown, DateTime.format("mm分ss秒", seconds * 1000));
//        });
        holder.txtMoney.setText("￥"+item.money);
        holder.txtOncePrice.setText("￥"+item.oncemoney);
        holder.txtOncePrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        holder.txtNum.setText("已售"+item.num);
        holder.txtTotal.setText("总份"+item.sellnum+"份");
    }
    public  void countdown(TextView btn,long millionseconds) {
        new CountDownTimer(millionseconds, 1000) {
            @Override
            public void onTick(long s) {
                btn.setText(DateTime.toTime(s));
            }

            @Override
            public void onFinish() {
                btn.setText("开始抢！");
            }
        }.start();
    }
    static class PanicViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_shop)
        AutofitImageView imgShop;
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_time)
        TextView txtTime;
        @BindView(R.id.txt_money)
        TextView txtMoney;
        @BindView(R.id.txt_num)
        TextView txtNum;
        @BindView(R.id.txt_once_price)
        TextView txtOncePrice;
        @BindView(R.id.txt_total)
        TextView txtTotal;



        public PanicViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
